package com.empresa.iflex.core;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.Toast;

import com.empresa.iflex.R;
import com.empresa.iflex.chart.AverageTemperatureChart;
import com.empresa.iflex.database.IFlexDbAdapter;

public class Tab extends TabActivity{
    private Resources res; 
    private TabHost tabHost; 
    private TabHost.TabSpec spec;  
    private Intent intent;
    IFlexDbAdapter ida;
    SQLiteDatabase db;
	
	/* Chamada quando o bot�o Menu � apertado */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu_tab, menu);
          return true;
    }
    
    
    /* Chamad quando uma op��o do Menu � selecionada */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      // Captura o menu selecionado
       switch (item.getItemId()) {
          case R.id.about:
             mAbout();
             return true;
          case R.id.delete_all:
        	  apagarTudo();
             return true;
          case R.id.settings:
        	  mSettings();
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }
    
    
    /* Opção "About" do Menu */
    private void mAbout(){  
    	Context mContext = getApplicationContext();
    	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layoutmenu = inflater.inflate(R.layout.about,
                (ViewGroup) findViewById(R.id.layoutmenu));

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);  
    	builder.setIcon(R.drawable.f);
    	builder.setTitle("iFlex");
	    builder.setView(layoutmenu)
	    	.setCancelable(false)
	    	.setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
	    		public void onClick(DialogInterface dialog, int id){  
	    			dialog.cancel();
	    	    }  
	    }); 
    	AlertDialog alert = builder.create();  
    	alert.show(); 
    	
    }
    
    /* Opção Configurações do Menu */
    private void mSettings(){  
    	Context mContext = getApplicationContext();
    	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layoutmenu = inflater.inflate(R.layout.check,
                (ViewGroup) findViewById(R.id.layoutcheck));

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);  
    	builder.setTitle("Configurações do Gráfico");
	    builder.setView(layoutmenu)
	    	.setCancelable(false)
	    	.setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
	    		public void onClick(DialogInterface dialog, int id){  
	    			dialog.cancel();
	    	    }  
	    }); 
	    builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener(){ 
    		public void onClick(DialogInterface dialog, int id){  
    			dialog.cancel();
    	    }  
	    }); 
    	AlertDialog alert = builder.create();  
    	alert.show(); 
    	
    }
	
	
    /* Chamada quando a tabela de abas � inicializada */
    public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.tab);

	    res = getResources(); 
	    tabHost = getTabHost();  

	    intent = new Intent().setClass(getApplicationContext(), Ver.class);		
	    spec = tabHost.newTabSpec("abastecimentos").setIndicator("Abastecimentos",
	                      res.getDrawable(R.drawable.tab1))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    
	    intent = new Intent().setClass(getApplicationContext(), Mes.class);
	    spec = tabHost.newTabSpec("mes").setIndicator("Mês",
	                      res.getDrawable(R.drawable.tab2))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    

	    AverageTemperatureChart chart = new AverageTemperatureChart();
	    intent = chart.execute(getApplicationContext());	    
	    //intent = new Intent().setClass(getApplicationContext(), Grafico.class);
	    spec = tabHost.newTabSpec("estatisticas").setIndicator("Estatisticas",
	                      res.getDrawable(R.drawable.tab3))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTabByTag("abastecimentos");
	}
    
    private void apagarTudo(){  
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);  
    	builder.setIcon(R.drawable.f);
	    builder.setMessage("Isso vai apagar todos os dados inseridos! Deseja continuar?")
	    	.setCancelable(false)
	    	.setPositiveButton("Sim", new DialogInterface.OnClickListener(){ 
	    		public void onClick(DialogInterface dialog, int id){  
	    			ida = new IFlexDbAdapter(getApplicationContext());
	    			db = ida.wOpen();
	    			
	    			int qtd_deleted = Percurso.deleteAll(db);
	    			if (qtd_deleted == 1){
	    				Toast.makeText(getApplicationContext(), "1 percurso foi removido!", Toast.LENGTH_SHORT).show();
	    			}else {
	    				Toast.makeText(getApplicationContext(), Integer.toString(qtd_deleted) +" percursos foram removidos!", Toast.LENGTH_SHORT).show();
	    			}
	    			finish(); //volta a tela incial
	    	    }  
	    });
	    builder.setNegativeButton("Não", new DialogInterface.OnClickListener(){  
  	          public void onClick(DialogInterface dialog, int id){  
  	        	Toast.makeText(getApplicationContext(), "Cancelado! Nenhum percurso foi removido.", Toast.LENGTH_SHORT).show();
  	          }  
  	     }); 
    	AlertDialog alert = builder.create();  
    	alert.show();  
	} 
    
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	if (db != null){
    		db.close();
    		ida.close();
    	}
    }
   
}
